package com.audaque.springboot.foshanupload.web.raspcloud.controller;

import com.audaque.springboot.foshanupload.authcore.controller.AbstractAuthenticationController;
import com.audaque.springboot.foshanupload.security.encoder.MyMd5PasswordEncoder;
import com.audaque.springboot.foshanupload.web.raspcloud.entity.User;
import com.audaque.springboot.foshanupload.web.raspcloud.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zgb
 * @desc ...
 * @date 2022-06-28 21:53:02
 */

@Slf4j
@RequestMapping("authentication")
@RestController
public class AuthenticationController extends AbstractAuthenticationController {

    @Autowired
    private IUserService userService;

    @Autowired
    private MyMd5PasswordEncoder passwordEncoder;



    @PostMapping("/register")
    public Object register(@RequestBody User user) {
        // 查询是否有相同用户名的用户，有则响应"该用户名已注册"
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User existUser = userService.getOne(queryWrapper);
        if (existUser != null) {
            return "该用户名已注册";
        }
        // 将密码进行加密操作
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        // 保存到数据库
        userService.save(user);
        // 查询数据库用户信息
        User savedUser = userService.getOne(queryWrapper);
        return savedUser;
    }

}
